package com.virtual.rerijaapps.pet.ui;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Typeface;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.TextView;

public class CustomTextViewVertical extends TextView
{
	private Context mContext;

	private int mStyle = 1;

    private static Typeface tf_bold;

    private static Typeface tf_cond_light;

    private static Typeface tf_count_light_italic;

    private boolean topDown = false;
    
	public CustomTextViewVertical( Context context )
	{
		super( context, null );
		mContext = context;
		init();
	}

	public CustomTextViewVertical( Context context, AttributeSet attrs )
	{
		super( context, attrs );
		mContext = context;
		mStyle = !isInEditMode() ? attrs.getAttributeIntValue( "http://schemas.android.com/apk/res/android", "textStyle", mStyle ) : mStyle; // NON-NLS.
		final int gravity = getGravity();
        if ( Gravity.isVertical( gravity )
            && ( gravity & Gravity.VERTICAL_GRAVITY_MASK ) 
            == Gravity.BOTTOM )
        {
            setGravity( 
                ( gravity & Gravity.HORIZONTAL_GRAVITY_MASK )
                    | Gravity.TOP );
            topDown = false;
        }
        else
        {
            topDown = true;
        }
		init();
	}

	protected void init()
	{
		if ( !isInEditMode() )
		{
            if ( 0 == mStyle )
            {
                if ( null == tf_cond_light )
                {
                    tf_cond_light = Typeface.createFromAsset( mContext.getAssets(), "fonts/OpenSans-CondLight.ttf" );
                }
                setTypeface( tf_cond_light );
            }
            else if ( 2 == mStyle )
            {
                if ( null == tf_count_light_italic )
                {
                    tf_count_light_italic = Typeface.createFromAsset( mContext.getAssets(), "fonts/OpenSans-CondLightItalic.ttf" );
                }
                setTypeface( tf_count_light_italic );
            }
            else
            {
                if ( null == tf_bold )
                {
                    tf_bold = Typeface.createFromAsset( mContext.getAssets(), "fonts/OpenSans-CondBold.ttf" );
                }
                setTypeface( tf_bold );
            }
		}
	}
	
	@Override
    protected void onMeasure( int widthMeasureSpec, int heightMeasureSpec )
    {
        super.onMeasure( heightMeasureSpec, widthMeasureSpec );
        setMeasuredDimension( getMeasuredHeight(), getMeasuredWidth() );
    }
 
    @Override
    protected void onDraw( Canvas canvas )
    {
        TextPaint textPaint = getPaint();
        textPaint.setColor( getCurrentTextColor() );
        textPaint.drawableState = getDrawableState();
        canvas.save();
        if ( topDown )
        {
            canvas.translate( getWidth(), 0 );
            canvas.rotate( 90 );
        }
        else
        {
            canvas.translate( 0, getHeight() );
            canvas.rotate( -90 );
        }
        canvas.translate( getCompoundPaddingLeft(), getExtendedPaddingTop() );
        getLayout().draw( canvas );
        canvas.restore();
    }
}
